﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;

namespace XCT.Forms
{
    public partial class FormCLLH : DevExpress.XtraEditors.XtraForm
    {
        public FormCLLH()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormCLLH(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region "Biến"

        clsCLLH clscl = new clsCLLH();
        clsDungChung clsdc = new clsDungChung();
        string TuanKH;
        int intTrangThai = -1; // 0:Them, 1:Sua, 2:Xoa
        DataSet dtsNhapCLLH, dtsXemCLLH;
               
        #endregion

        #region "Sự Kiện"

        #region "Dùng Chung"

        private void FormCLLH_Load(object sender, EventArgs e)
        {
            fKhoiTaoComBo();
        }

        private void FormCLLH_FormClosed(object sender, FormClosedEventArgs e)
        {
            formMain.pFormCLLH = null;
        }

        private void FormCLLH_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.S:
                            if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
                            {
                                if (intTrangThai != -1)
                                    if (XtraMessageBox.Show("Bạn có chắc muốn lưu không ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        grdNhapCLLH_View.FocusedRowHandle = grdNhapCLLH_View.FocusedRowHandle + 1;
                                        grdNhapCLLH_View.CloseEditor();
                                        if (fLuuCLLH() == true)
                                        {
                                            XtraMessageBox.Show("Lưu thành công !", "Thông báo:");
                                            intTrangThai = -1;
                                            fDocChietTinhCLLHTheoLSX(lupLSX.Text, dteNgayTH.Text);
                                        }
                                        else
                                        {
                                            XtraMessageBox.Show("Không thể lưu, vui lòng kiểm tra lại dữ liệu nhập!");
                                        }
                                    }
                            }
                            return;
                        case (Keys.E):
                            if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
                            {
                                if (intTrangThai == -1 || intTrangThai == 1)
                                {
                                    ArrayList arr = new ArrayList();
                                    arr.Add("SL_Nhap_Ca1"); //cột sản lượng ca 1
                                    arr.Add("SL_Nhap_Ca2"); //cột sản lượng ca 2
                                    arr.Add("SL_Nhap_Ca3"); //cột sản lượng ca 3
                                    arr.Add("GhiChu"); //ghi chú

                                    clsdc.editGrid(grdNhapCLLH_View, true, arr);
                                    fMoKhoaControl(false);

                                    intTrangThai = 1;
                                    lblMsg.Text = "Bạn đang ở chế độ sửa, vui lòng nhấn [Ctrl+S] để lưu !"; lblMsg.Visible = true;
                                }
                                else
                                {
                                    XtraMessageBox.Show("Bạn phải lưu trước khi chuyển thao tác mới...", "Thông Báo:");
                                }
                            }
                            return;
						case (Keys.P):
                            if (XtraMessageBox.Show("Bạn có chắc muốn xuất ra tập tin Excel ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes) 
                                XuatTapTinExcel(tabCLLH.SelectedTabPage.Name);
                            return;
                    }
                }
                else if (e.KeyCode == Keys.F5)
                {
                    intTrangThai = -1;
                    if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
                    {
                        fDocChietTinhCLLHTheoLSX(lupLSX.Text, dteNgayTH.Text);
                    }
                }
                else if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        //Sự kiện SelectedIndexChanged của cmbTuanKH, cmbThangKH, cmbNamKH
        private void cmbNamKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            TuanKH = cmbTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
            if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
            {
                fDocLSXTheoTuanKH(TuanKH);
                //fDocChietTinhCLLHTheoLSX(lupLSX.Text, dteNgayTH.Text);
            }
            else if (tabCLLH.SelectedTabPage.Name == "tabCLLH2")
            {
                fDocCLLHTheoTuanKH(TuanKH);
                //fDocCLLHTheoLSX(lupLSX.Text);
            }
            lupLSX_EditValueChanged(sender, e);
        }

        //Tìm LSX
        private void txtTimLSX_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtTimLSX.Text.Length > 0)
                {
                    DataSet ds = new DataSet();
                    clscl.SoLSX = txtTimLSX.Text.Trim();
                    ds = clscl.TimLSX(tabCLLH.SelectedTabPage.Name);
                    lupLSX.Properties.DataSource = ds.Tables[0].DefaultView;
                    lupLSX.Properties.ValueMember = "SoLSX";
                    lupLSX.Properties.DisplayMember = "SoLSX";
                    lupLSX.ItemIndex = 0;
                    lupLSX_EditValueChanged(sender, e);

                    if (lupLSX.ItemIndex != -1)
                    {
                        cmbTuanKH.EditValue = lupLSX.GetColumnValue("TTN").ToString().Substring(0, 2);
                        cmbThangKH.EditValue = lupLSX.GetColumnValue("TTN").ToString().Substring(2, 2);
                        cmbNamKH.EditValue = lupLSX.GetColumnValue("TTN").ToString().Substring(4, 4);
                    }
                }
            }
        }

        //Sự kiện EditValueChanged của lupLSX, dteNgayTH
        private void lupLSX_EditValueChanged(object sender, EventArgs e)
        {
            if (lupLSX.ItemIndex == -1)
                lblMaSP.Text = "Mã sản phẩm";
            else
                lblMaSP.Text = lupLSX.GetColumnValue("MaSP").ToString();

            if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
            {
                fDocChietTinhCLLHTheoLSX(lupLSX.Text, dteNgayTH.Text);
            }
            else if (tabCLLH.SelectedTabPage.Name == "tabCLLH2")
            {
                fDocCLLHTheoLSX(lupLSX.Text);
            }
        }

        //Sự kiện kiểm tra Tab trước khi chuyển Tab
        private void tabCLLH_SelectedPageChanging(object sender, DevExpress.XtraTab.TabPageChangingEventArgs e)
        {
            if (intTrangThai != -1)
            {
                XtraMessageBox.Show("Vui lòng nhấn [Ctrl+S] để lưu trước khi chuyển tab ...", "Thông Báo:");
                e.Cancel = true;
            }
        }

        //Sự kiện chuyển Tab
        private void tabCLLH_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            txtTimLSX.Text = "";
            lblMaSP.Text = "Mã sản phẩm";
            cmbNamKH_SelectedIndexChanged(sender, e);

            if (tabCLLH.SelectedTabPage.Name == "tabCLLH1")
            {
                dteNgayTH.Visible = true;
                labelNgayTH.Visible = true;
                //fDocLSXTheoTuanKH(TuanKH);
                //fDocChietTinhCLLHTheoLSX(lupLSX.Text, dteNgayTH.Text);
            }
            else if (tabCLLH.SelectedTabPage.Name == "tabCLLH2")
            {
                dteNgayTH.Visible = false;
                labelNgayTH.Visible = false;
                //fDocCLLHTheoTuanKH(TuanKH);
                //fDocCLLHTheoLSX(lupLSX.Text);
            }
        } 
    
        #endregion

        #region "tabCLLH1"

        private void repositoryItemSpinEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '-')
            {
                e.Handled = true;
            }
        }

        private void grdNhapCLLH_View_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            float ConLai, SL_Nhap_Ca1 = 0, SL_Nhap_Ca2 = 0, SL_Nhap_Ca3 = 0;
            if (dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca1"].ToString() != "")
                SL_Nhap_Ca1 = float.Parse(dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca1"].ToString());
            if (dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca2"].ToString() != "")
                SL_Nhap_Ca2 = float.Parse(dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca2"].ToString());
            if (dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca3"].ToString() != "")
                SL_Nhap_Ca3 = float.Parse(dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["SL_Nhap_Ca3"].ToString());
            ConLai = float.Parse(dtsNhapCLLH.Tables[0].Rows[e.RowHandle]["ConLai"].ToString());

            if (SL_Nhap_Ca1 + SL_Nhap_Ca2 + SL_Nhap_Ca3 > ConLai)
            {
                XtraMessageBox.Show("Bạn nhập vượt quá kế hoạch thực hiện !");
                grdNhapCLLH_View.SetFocusedValue(0);
                grdNhapCLLH_View.FocusedColumn = grdNhapCLLH_View.VisibleColumns[grdNhapCLLH_View.FocusedColumn.VisibleIndex - 1];
            }
        }     

        #endregion

        #region "tabCLLH2"


        #endregion

        #endregion
        
        #region "Hàm"

        #region "Dùng Chung"

        private void fMoKhoaControl(Boolean bisMoKhoa)
        {
            cmbTuanKH.Enabled = bisMoKhoa;
            cmbThangKH.Enabled = bisMoKhoa;
            cmbNamKH.Enabled = bisMoKhoa;
            lupLSX.Enabled = bisMoKhoa;
            dteNgayTH.Enabled = bisMoKhoa;
            txtTimLSX.Enabled = bisMoKhoa;
        }

        private void fKhoiTaoComBo()
        {
            for (int i = 2010; i <= DateTime.Now.Year + 1; i++)
                cmbNamKH.Properties.Items.Add(i.ToString());
            cmbNamKH.Text = DateTime.Now.Year.ToString();
            cmbThangKH.SelectedIndex = DateTime.Now.Month - 1;
            dteNgayTH.DateTime = DateTime.Now;
            TuanKH = cmbTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
        } 

        private void XuatTapTinExcel(string tabName)
        {
            SaveFileDialog saveDlg;
            try
            {
                saveDlg = new SaveFileDialog();
                saveDlg.InitialDirectory = "C:\"";
                saveDlg.Filter = "Excel Files|*.xls";
                saveDlg.FilterIndex = 2;
                if (saveDlg.ShowDialog() == DialogResult.OK)
                {
                    if (tabName == "tabCLLH1")
                    {
                        DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                        grdNhapCLLH_View.ExportToXls(saveDlg.FileName, options);
                    }
                    else if (tabName == "tabCLLH2")
                    {
                        DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                        grdXemCLLH_View.ExportToXls(saveDlg.FileName, options);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                saveDlg = null;
            }
        }
		
        #endregion

        #region "tabCLLH1"

        //Khởi tạo grdNhapCLLH_View
        private void KhoiTaogrdNhapCLLH(DevExpress.XtraGrid.Views.Grid.GridView grdView)
        {
            grdView.Columns["MaSP"].Visible = false; //MaSP
            grdView.Columns["MaSP"].Caption = "Maû SP";
            grdView.Columns["ThucHien"].AppearanceCell.BackColor = System.Drawing.Color.SpringGreen;
            grdView.Columns["ThucHien"].Width = 70; //Thuc hien
            grdView.Columns["ThucHien"].Caption = "Thöïc hieän";
            grdView.Columns["LuyKe"].Visible = false; //Luy ke
            grdView.Columns["LuyKe"].Caption = "Luõy keá";
            grdView.Columns["ConLai"].AppearanceCell.BackColor = System.Drawing.Color.Orange;
            grdView.Columns["ConLai"].Width = 65; //Còn Lại
            grdView.Columns["ConLai"].Caption = "Coøn laïi";
            grdView.Columns["SL_Nhap_Ca1"].AppearanceCell.BackColor = System.Drawing.Color.SkyBlue;
            grdView.Columns["SL_Nhap_Ca1"].Width = 65; //san luong ca A
            grdView.Columns["SL_Nhap_Ca1"].Caption = "SL Ca A";
            grdView.Columns["SL_Nhap_Ca2"].AppearanceCell.BackColor = System.Drawing.Color.Pink;
            grdView.Columns["SL_Nhap_Ca2"].Width = 65; //san luong ca B
            grdView.Columns["SL_Nhap_Ca2"].Visible = false; //san luong ca B
            grdView.Columns["SL_Nhap_Ca2"].Caption = "SL Ca B";//*
            grdView.Columns["SL_Nhap_Ca3"].AppearanceCell.BackColor = System.Drawing.Color.Silver;
            grdView.Columns["SL_Nhap_Ca3"].Width = 65; //san luong ca C
            grdView.Columns["SL_Nhap_Ca3"].Visible = false; //san luong ca C
            grdView.Columns["SL_Nhap_Ca3"].Caption = "SL Ca C";
            grdView.Columns["Tong_SL_Nhap"].Visible = false; //Tong san luong nhap
            grdView.Columns["Tong_SL_Nhap"].Caption = "Toång SL";
            grdView.Columns["MaChungLoai_Mousse"].Width = 75; // ma chung loai
            grdView.Columns["MaChungLoai_Mousse"].Caption = "Chuûng loaïi";
            grdView.Columns["TenChungLoai_Mousse"].Visible = false; //Ten Chung Loai Mousse
            grdView.Columns["TenChungLoai_Mousse"].Caption = "Tên chuûng loaïi mousse";
            grdView.Columns["MaMau_Mousse"].Width = 150; //ma mau mousse
            grdView.Columns["MaMau_Mousse"].Caption = "Maõ maøu mousse";
            grdView.Columns["DoLy"].Width = 50; //do ly
            grdView.Columns["DoLy"].Caption = "Ñoä ly";
            grdView.Columns["QuiCach"].Width = 90; //qui cach
            grdView.Columns["QuiCach"].Caption = "Qui caùch";
            grdView.Columns["MaHoaVan_Mousse"].Width = 65; //Ma hoa van Mousse
            grdView.Columns["MaHoaVan_Mousse"].Caption = "Maõ hoa vaên";
            grdView.Columns["MaHoaVan_Mousse"].Visible = false;
            grdView.Columns["TenHoaVan"].Width = 65; //Ma hoa van Mousse
            grdView.Columns["TenHoaVan"].Caption = "Hoa vaên";
            grdView.Columns["DVTID"].Visible = false; //DvtID
            grdView.Columns["DVTID"].Caption = "Maõ ÑVT";
            grdView.Columns["Ten_DVT"].Width = 65; //don vi tính
            grdView.Columns["Ten_DVT"].Caption = "ÑV tính";
            grdView.Columns["MaCD"].Visible = false; //Ma cong doan
            grdView.Columns["MaCD"].Caption = "Maõ coâng ñoaïn";
            grdView.Columns["TenCD"].Visible = false; //Ten cong doan
            grdView.Columns["TenCD"].Caption = "Teân coâng ñoaïn";
            grdView.Columns["DinhMuc"].Visible = false; //Dinh muc
            grdView.Columns["DinhMuc"].Caption = "Ñònh möùc";
            grdView.Columns["DoShore"].Width = 65; //Do Shore
            grdView.Columns["DoShore"].Caption = "Ñoä shore"; //Do Shore
            grdView.Columns["GhiChuChiTiet"].Visible = false; //Ghi chu chi tiet
            grdView.Columns["GhiChuChiTiet"].Caption = "Ghi chuù chi tieát";
            grdView.Columns["GhiChu"].AppearanceCell.BackColor = System.Drawing.Color.Yellow;
            grdView.Columns["GhiChu"].Width = 150; //Ghi chu
            grdView.Columns["GhiChu"].Caption = "Ghi chuù";
            grdView.Columns["CoSoID"].Visible = false; //Cỡ Số ID
            grdView.Columns["CoSoID"].Caption = "Côõ soá ID";
            grdView.Columns["MaCoSo"].Visible = false; //Mã Cỡ Số
            grdView.Columns["MaCoSo"].Caption = "Côõ soá";

            grdView.Columns["ThucHien"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["ThucHien"].DisplayFormat.FormatString = "n1";
            grdView.Columns["LuyKe"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["LuyKe"].DisplayFormat.FormatString = "n1";
            grdView.Columns["ConLai"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["ConLai"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca1"].ColumnEdit = repositoryItemSpinEdit1;
            grdView.Columns["SL_Nhap_Ca1"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca1"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca2"].ColumnEdit = repositoryItemSpinEdit1;
            grdView.Columns["SL_Nhap_Ca2"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca2"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca3"].ColumnEdit = repositoryItemSpinEdit1;
            grdView.Columns["SL_Nhap_Ca3"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca3"].DisplayFormat.FormatString = "n1";
            grdView.Columns["Tong_SL_Nhap"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["Tong_SL_Nhap"].DisplayFormat.FormatString = "n1";
            grdView.Columns["DoLy"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["DoLy"].DisplayFormat.FormatString = "n1";
        }

        //Đọc SoLSX của điều phối theo tuần KH
        private void fDocLSXTheoTuanKH(string TuanKH)
        {
            DataSet ds = new DataSet();
            ds = clscl.DocLSXTheoTuanKH(TuanKH);
            lupLSX.Properties.DataSource = ds.Tables[0].DefaultView;
            lupLSX.Properties.ValueMember = "SoLSX";
            lupLSX.Properties.DisplayMember = "SoLSX";
        }    

        //Đọc chiết tính theo lệnh sản xuất, ngày thực hiện
        private void fDocChietTinhCLLHTheoLSX(string SoLSX, string NgayTH)
        {
            dtsNhapCLLH = new DataSet();
            clscl.SoLSX = SoLSX;
            dtsNhapCLLH = clscl.DocChietTinhCLLHTheoLSX(NgayTH);
            grdNhapCLLH.DataSource = dtsNhapCLLH.Tables[0].DefaultView;

            lblMsg.Visible = false;

            fMoKhoaControl(true);
            KhoiTaogrdNhapCLLH(grdNhapCLLH_View);
            clsdc.editGrid(grdNhapCLLH_View, false, null);
        }

        // Lưu CLLH theo trạng thái
        private Boolean fLuuCLLH()
        {
            try
            {
                switch (intTrangThai)
                {
                    case 1: //trang thai = 1: sửa
                        if (dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows.Count > 0)
                        {
                            for (int intRow = 0; intRow < dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows.Count; intRow++)
                            {
                                clscl = new clsCLLH();
                                clscl.SoLSX = lupLSX.EditValue.ToString();
                                clscl.MaSP = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["MaSP"].ToString();
                                //clscl.TuanKH = TuanKH; //không lưu tuần KH, lấy bên điều phối
                                clscl.NgayTH = Convert.ToDateTime(dteNgayTH.DateTime.ToShortDateString());
                                clscl.SLKH = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["ThucHien"].ToString());
                                if (dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca1"].ToString() != "")
                                    clscl.SL_Nhap_Ca1 = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca1"].ToString());
                                if (dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca2"].ToString() != "")
                                    clscl.SL_Nhap_Ca2 = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca2"].ToString());
                                if (dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca3"].ToString() != "")
                                    clscl.SL_Nhap_Ca3 = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["SL_Nhap_Ca3"].ToString());
                                clscl.Tong_SL_Nhap = clscl.SL_Nhap_Ca1 + clscl.SL_Nhap_Ca2 + clscl.SL_Nhap_Ca3;
                                clscl.MaChungLoai_Mousse = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["MaChungLoai_Mousse"].ToString();
                                clscl.MaMau_Mousse = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["MaMau_Mousse"].ToString();
                                clscl.DoLy = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["DoLy"].ToString());
                                clscl.QuiCach = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["QuiCach"].ToString();
                                clscl.MaHoaVan_Mousse = int.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["MaHoaVan_Mousse"].ToString());
                                clscl.DvtID = int.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["DVTID"].ToString());
                                clscl.MaCD = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["MaCD"].ToString();
                                clscl.DinhMuc = float.Parse(dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["DinhMuc"].ToString());
                                clscl.DoShore = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["DoShore"].ToString();
                                clscl.GhiChuChiTiet = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["GhiChuChiTiet"].ToString();
                                clscl.GhiChu = dtsNhapCLLH.Tables[0].GetChanges(DataRowState.Modified).Rows[intRow]["GhiChu"].ToString();
                                clscl.UserUpdate = clsDungChung.strUser;

                                if (clscl.LuuCLLH() == false)
                                    return false;
                            }
                            return true;
                        }
                        return true;
                    default:
                        return true;
                }
            }
            catch (Exception ex)
            {                return false;
            }
        }

        #endregion

        #region "tabCLLH2"

        //Khởi tạo grdXemCLLH_View
        private void KhoiTaogrdXemCLLH(DevExpress.XtraGrid.Views.Grid.GridView grdView)
        {
            grdView.Columns["MaSP"].Visible = false; //MaSP
            grdView.Columns["MaSP"].Caption = "Maû SP";
            grdView.Columns["ThucHien"].AppearanceCell.BackColor = System.Drawing.Color.SpringGreen;
            grdView.Columns["ThucHien"].Width = 70; //Thuc hien
            grdView.Columns["ThucHien"].Caption = "Thöïc hieän";
            grdView.Columns["Tong_SL_Nhap"].AppearanceCell.BackColor = System.Drawing.Color.Orange;
            grdView.Columns["Tong_SL_Nhap"].Width = 70; //Tổng San luong
            grdView.Columns["Tong_SL_Nhap"].Caption = "Toång SL";
            grdView.Columns["SL_Nhap_Ca1"].AppearanceCell.BackColor = System.Drawing.Color.SkyBlue;
            grdView.Columns["SL_Nhap_Ca1"].Width = 65; //san luong ca A
            grdView.Columns["SL_Nhap_Ca1"].Caption = "SL Ca A";
            grdView.Columns["SL_Nhap_Ca2"].AppearanceCell.BackColor = System.Drawing.Color.Pink;
            grdView.Columns["SL_Nhap_Ca2"].Width = 65; //san luong ca B
            grdView.Columns["SL_Nhap_Ca2"].Visible = false; //san luong ca B
            grdView.Columns["SL_Nhap_Ca2"].Caption = "SL Ca B";
            grdView.Columns["SL_Nhap_Ca3"].AppearanceCell.BackColor = System.Drawing.Color.Silver;
            grdView.Columns["SL_Nhap_Ca3"].Width = 65; //san luong ca C
            grdView.Columns["SL_Nhap_Ca3"].Visible = false; //san luong ca C
            grdView.Columns["SL_Nhap_Ca3"].Caption = "SL Ca C";
            grdView.Columns["NgayTH"].Width = 100; //Ngay thuc hien
            grdView.Columns["NgayTH"].Caption = "Ngaøy thöïc hieän";
            grdView.Columns["MaChungLoai_Mousse"].Width = 75; //chung loai
            grdView.Columns["MaChungLoai_Mousse"].Caption = "Chuûng loaïi";
            grdView.Columns["TenChungLoai_Mousse"].Visible = false; //Ten Chung Loai Mousse
            grdView.Columns["TenChungLoai_Mousse"].Caption = "Tên chuûng loaïi mousse";
            grdView.Columns["MaMau_Mousse"].Width = 150; //ma mau mousse
            grdView.Columns["MaMau_Mousse"].Caption = "Maõ maøu mousse";
            grdView.Columns["DoLy"].Width = 50; //do ly
            grdView.Columns["DoLy"].Caption = "Ñoä ly";
            grdView.Columns["QuiCach"].Width = 90; //qui cach
            grdView.Columns["QuiCach"].Caption = "Qui caùch";
            grdView.Columns["MaHoaVan_Mousse"].Width = 65; //Ma hoa van Mousse
            grdView.Columns["MaHoaVan_Mousse"].Caption = "Maõ hoa vaên";
            grdView.Columns["MaHoaVan_Mousse"].Visible = false;
            grdView.Columns["TenHoaVan"].Width = 65; //Ma hoa van Mousse
            grdView.Columns["TenHoaVan"].Caption = "Hoa vaên";
            grdView.Columns["DVTID"].Visible = false; //DvtID
            grdView.Columns["DVTID"].Caption = "Maõ ÑVT";
            grdView.Columns["Ten_DVT"].Width = 65; //don vi tính
            grdView.Columns["Ten_DVT"].Caption = "ÑV tính";
            grdView.Columns["MaCD"].Visible = false; //Ma cong doan
            grdView.Columns["MaCD"].Caption = "Maõ coâng ñoaïn";
            grdView.Columns["TenCD"].Visible = false; //Ten cong doan
            grdView.Columns["TenCD"].Caption = "Teân coâng ñoaïn";
            grdView.Columns["DinhMuc"].Visible = false; //Dinh muc
            grdView.Columns["DinhMuc"].Caption = "Ñònh möùc";
            grdView.Columns["DoShore"].Width = 65; //Do Shore
            grdView.Columns["DoShore"].Caption = "Ñoä shore"; //Do Shore
            grdView.Columns["GhiChuChiTiet"].Visible = false; //Ghi chu chi tiet
            grdView.Columns["GhiChuChiTiet"].Caption = "Ghi chuù chi tieát";
            grdView.Columns["GhiChu"].Width = 150; //Ghi chu
            grdView.Columns["GhiChu"].Caption = "Ghi chuù";
            grdView.Columns["CoSoID"].Visible = false; //Cỡ Số ID
            grdView.Columns["CoSoID"].Caption = "Côõ soá ID";
            grdView.Columns["MaCoSo"].Visible = false; //Mã Cỡ Số
            grdView.Columns["MaCoSo"].Caption = "Côõ soá";

            grdView.Columns["ThucHien"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["ThucHien"].DisplayFormat.FormatString = "n1";            
            grdView.Columns["Tong_SL_Nhap"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["Tong_SL_Nhap"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca1"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca1"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca2"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca2"].DisplayFormat.FormatString = "n1";
            grdView.Columns["SL_Nhap_Ca3"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap_Ca3"].DisplayFormat.FormatString = "n1";
            grdView.Columns["DoLy"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["DoLy"].DisplayFormat.FormatString = "n1";
        }

        //Đọc SoLSX của CLLH theo tuần KH
        private void fDocCLLHTheoTuanKH(string TuanKH)
        {
            DataSet ds = new DataSet();
            ds = clscl.DocCLLHTheoTuanKH(TuanKH);
            lupLSX.Properties.DataSource = ds.Tables[0].DefaultView;
            lupLSX.Properties.ValueMember = "SoLSX";
            lupLSX.Properties.DisplayMember = "SoLSX";
        }    

        //Đọc CLLH theo lệnh sản xuất
        private void fDocCLLHTheoLSX(string SoLSX)
        {
            dtsXemCLLH = new DataSet();
            dtsXemCLLH = clscl.DocCLLHTheoLSX(SoLSX);
            grdXemCLLH.DataSource = dtsXemCLLH.Tables[0].DefaultView;

            fMoKhoaControl(true);
            KhoiTaogrdXemCLLH(grdXemCLLH_View);
            clsdc.editGrid(grdXemCLLH_View, false, null);
        }

        #endregion                       
        
        #endregion
    }
}